package com.registration.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.registration.Annotation.OperateLog;
import com.registration.bean.RoleBean;
import com.registration.service.RoleService;
import com.registration.util.JsonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/RoleController")
public class RoleController {

    @Autowired
    RoleService roleService;

    JsonResponse jsonResponse=new JsonResponse();

    @GetMapping("/getRoleList")
    public JsonResponse getRoleList() {
        return JsonResponse.success(roleService.getRoleList());
    }


    @PostMapping("/insertRole")
    @OperateLog("添加角色")
    @SaCheckPermission("sys:home:role:add")
    public JsonResponse insertRole(@RequestBody RoleBean roleBean) {
        return JsonResponse.success(roleService.insertRole(roleBean));
    }


    @DeleteMapping("/deleteRole/{role_id}")
    @OperateLog("删除角色")
    @SaCheckPermission("sys:home:role:delete")
    public JsonResponse deleteRole(@PathVariable Integer role_id) {
        return JsonResponse.success(roleService.deleteRole(role_id));
    }

    @DeleteMapping("/batchDelete")
    @OperateLog("批量删除")
    @SaCheckPermission("sys:home:role:batchdelete")
    public JsonResponse batchDelete(@RequestBody RoleBean roleBean) {
        List<Integer> batchDelete = roleBean.getBatchDeleteList();
         for (Integer role_id : batchDelete){
             roleService.deleteRole(role_id);
         }
         return JsonResponse.success(null);
    }

    @PutMapping("/updateRole")
    @OperateLog("更新角色列表")
    @SaCheckPermission("sys:home:role:update")
    public JsonResponse updateRole(@RequestBody RoleBean roleBean) {
        return JsonResponse.success(roleService.updateRole(roleBean));
    }



    @GetMapping("/getRoleList/{pageNum}/{pageSize}")
    @OperateLog("查看角色列表")
    @SaCheckPermission("sys:home:role:view")
    public JsonResponse getRoleList(@PathVariable Integer pageNum, @PathVariable Integer pageSize, RoleBean roleBean) {

        return JsonResponse.success(roleService.getRoleList(pageNum, pageSize, roleBean));
    }
}
